class UserController < ApplicationController
  
  def login
     @user = User.new
     @user.Username = params[:username]
  end
  
  def sendmess
    @m = Message.new()
    @m.Content = params[:newmess]
    @m.Title = params[:title]
    @m.Uid= session[:uid]
    @m.Type = 0
    @m.Date_sent = Time.now
    if @m.save
      redirect_to :action=>'message',:alert1 => "One message has been sent"
    else
      redirect_to :action=>'message',:alert => "Send message fail"
    end
  end
  
  def viewdetail
    session[:md]= Message.find_by_id(params[:mes])
    redirect_to :action=>'message'
  end
  def message
    @bam = 5
    if session[:uid].nil?
        redirect_to :action=>"login", :controller=>"user"
      end
    @fromAdmin = Message.where(:Type=>1,:Uid=>session[:uid]) 
  end
  
  def daily_update
    @unique=Check.find_by_date()
    if @unique.nil?
      @unique=Check.new
      @unique.date=Date.today
      @unique.save
      @br=Borrowingreport.all
      @br.each do |b|
        if b.Borrow_date.past?
          @target=User.find_by_id(b.Uid)
          @target.Penalty=@target.Penalty+1
        end
      end
    end
  end

  def verify
    if @user = User.find_by_Username_and_Password(params[:username],params[:password])
        session[:uid] = @user.id 
        if @user.Username.to_s=='admin'
         # daily_update
          session[:admin] = @user.id 
          redirect_to newbook_path
          return
        end
        
        redirect_to :controller => "search", :action => 'search_view'
    else
        redirect_to :action => 'login', :username => params[:username], :alert=>"Invalid username or password"
    end
  end
  
  def xoamessage
    @m = session[:md]
    @m.destroy
    session[:md]=nil
    redirect_to :action=>'message'
  end
end
